import request from '@/utils/request'

// 数据库备份相关接口
export const database = {
  // 获取数据库表列表
  getDatabaseList: () => {
    return request({
      url: '/api/home/database/list',
      method: 'get',
    })
  },

  // 备份数据库
  backupDatabase: (data?: any) => {
    return request({
      url: '/api/home/database/backup',
      method: 'post',
      data,
    })
  },

  // 恢复数据库
  restoreDatabase: (data: { time?: number | string }) => {
    return request({
      url: '/api/home/database/restore',
      method: 'post',
      data,
    })
  },

  // 删除数据库备份
  deleteBackup: (data: { time?: number | string; lock?: number }) => {
    return request({
      url: '/api/home/database/del',
      method: 'delete',
      data,
    })
  },

  // 获取备份文件列表
  getBackupList: () => {
    return request({
      url: '/api/home/database/backup_list',
      method: 'get',
    })
  },

  // 优化表
  optimizeTables: (data?: any) => {
    return request({
      url: '/api/home/database/optimize',
      method: 'post',
      data,
      timeout: 300000, // 设置超时时间为5分钟
    })
  },

  // 修复表
  repairTables: (data?: any) => {
    return request({
      url: '/api/home/database/repair',
      method: 'post',
      data,
      timeout: 300000, // 设置超时时间为5分钟
    })
  },
}

// 向后兼容方法
export const getDatabaseList = database.getDatabaseList
export const backupDatabase = database.backupDatabase
export const restoreDatabase = database.restoreDatabase
export const deleteBackup = database.deleteBackup
export const getBackupList = database.getBackupList
export const optimizeTables = database.optimizeTables
export const repairTables = database.repairTables

// 为了匹配 DatabaseList.vue 中使用的函数名
export const getDatabaseTables = database.getDatabaseList
export const backupTables = database.backupDatabase
export const importBackup = database.restoreDatabase
